package com.knowif.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.knowif.dao.TopicMapper;
import com.knowif.pojo.Topic;
import com.knowif.pojo.TopicExample;
import com.knowif.system.model.RestBean;

/**
 * @author xingyao
 * @Description: 话题管理dao服务层
 */
@Service
public class Topic_service {

    @Autowired
    private TopicMapper topicMapper;

    /**
     * 话题查询
     * @param topicExample
     * @return
     */
    public List<Topic> selectTopic(TopicExample topicExample){
        return topicMapper.selectByExample(topicExample);
    }
    
    
    /**
     * @author: xingyao 
     * @Description:  通过topicIds查询topic集合
     * @param topicIds
     * @return
     */
    public List<Topic> selectTopic(List<Integer> topicIds){
    	if(topicIds == null || topicIds.isEmpty()){
    		return new ArrayList<Topic>();
    	}
    	TopicExample topicExample = new TopicExample();
    	topicExample.createCriteria().andIdIn(topicIds);
    	return topicMapper.selectByExample(topicExample);
    }
 
    /**
     * @author: xingyao 
     * @Description:  通过topicIds查询topic集合
     * @param topicIds
     * @return
     */
    public Topic selectTopic(Integer topicId){
    	if(topicId == null){
    		return null;
    	}
    	return topicMapper.selectByPrimaryKey(topicId);
    }
    
    
    public List<Topic> selectTopicByUserId(Integer userId){
    	if(userId == null){
    		return new ArrayList<Topic>();
    	}
    	TopicExample topicExample = new TopicExample();
    	topicExample.createCriteria().andUserIdEqualTo(userId);
    	return topicMapper.selectByExample(topicExample);
    }
    
    
    public List<Topic> selectTopicByUserIds(List<Integer> userIds){
    	if(userIds == null || userIds.isEmpty()){
    		return new ArrayList<Topic>();
    	}
    	TopicExample topicExample = new TopicExample();
    	topicExample.createCriteria().andUserIdIn(userIds);
    	return topicMapper.selectByExample(topicExample);
    }
    
    /**
     * 保存话题
     * @param topic
     * @return
     */
    public RestBean<?> saveTopic(Topic topic){
    	if(topic == null){
    		return RestBean.buildError("请输入要添加的topic");
    	}
        topicMapper.insert(topic);
        return RestBean.buildSuccess("添加话题成功");
    }

    /**
     * 更新不为空的topic属性
     * @param topic
     * @return
     */
    public RestBean<?> updateTopicNotNull(Topic topic){
    	if(topic == null){
    		return RestBean.buildError("更新topic信息失败");
    	}
    	topicMapper.updateByPrimaryKeySelective(topic);
        return RestBean.buildSuccess("更新topic信息成功");
    }

    /**
     * 更新所有topic属性
     * @param topic
     * @return
     */
    public RestBean<?> updateTopic(Topic topic){
    	if(topic == null){
    		return RestBean.buildError("更新topic信息失败");
    	}
        topicMapper.updateByPrimaryKey(topic);
        return RestBean.buildSuccess("更新topic信息成功");
    }

    /**
     * 删除topic
     * @param topicId
     */
    public RestBean<?> deleteTopic(Integer topicId){
    	if(topicId == null){
    		return RestBean.buildError("请输入要删除topic的id");
    	}
        topicMapper.deleteByPrimaryKey(topicId);
        return RestBean.buildSuccess("删除数据成功");
    }
    
    
    public RestBean<?> deleteTopics(List<Integer> topics){
    	if(topics == null){
    		return RestBean.buildError("请输入要删除的topic的id");
    	}
    	TopicExample topicExample = new TopicExample();
    	topicExample.createCriteria().andIdIn(topics);
    	topicMapper.deleteByExample(topicExample);
    	return RestBean.buildSuccess("删除数据成功");
    }
    
    
    /**
     * @author: xingyao 
     * @Description:  通过导师的userId删除topic
     * @param userId
     * @return
     */
    public RestBean<?> deleteByUserId(Integer userId){
    	if(userId == null){
    		return RestBean.buildError("请输入要删除的userid");
    	}
    	TopicExample topicExample = new TopicExample();
    	topicExample.createCriteria().andUserIdEqualTo(userId);
    	topicMapper.deleteByExample(topicExample);
    	return RestBean.buildSuccess("删除数据成功");
    }
    
    
    /**
     * @author: xingyao 
     * @Description:  批量通过导师的userId删除topic
     * @param userId
     * @return
     */
    public RestBean<?> deleteByUserIds(List<Integer> userIds){
    	if(userIds == null || userIds.isEmpty()){
    		return RestBean.buildError("请输入要删除的userids");
    	}
    	TopicExample topicExample = new TopicExample();
    	topicExample.createCriteria().andUserIdIn(userIds);
    	topicMapper.deleteByExample(topicExample);
    	return RestBean.buildSuccess("删除数据成功");
    }

    public void updateTopidDelNumById(Topic topic){
    	if(topic == null){
    		return;
    	}
    	topicMapper.updateByPrimaryKeySelective(topic);
    }
}
